package com.ssy.mall.controller;

import com.ssy.mall.common.ApiRestResponse;
import com.ssy.mall.filter.UserFilter;
import com.ssy.mall.model.request.CartReq;
import com.ssy.mall.model.request.CartSelectReq;
import com.ssy.mall.model.vo.CartVO;
import com.ssy.mall.service.CartService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: 苏少有的idea
 * @date: 2022/1/4
 * @description 购物车Controller
 */
@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;

    @ApiOperation("商品购物车列表")
    @GetMapping("/list")
    public ApiRestResponse list() {
        //内部获取用户ID,防止横向越权
        List<CartVO> cartList = cartService.list(UserFilter.currentUser.getId());
        return ApiRestResponse.success(cartList);

    }

    @ApiOperation("添加商品到购物车")
    @PostMapping("/add")
    public ApiRestResponse add(@RequestBody @Validated CartReq cartReq) {
        List<CartVO> cartVOList = cartService.add(UserFilter.currentUser.getId(), cartReq.getProductId(),cartReq.getCount());
        return ApiRestResponse.success(cartVOList);

    }

    @ApiOperation("更新购物车")
    @PostMapping("/update")
    public ApiRestResponse update(@RequestBody @Validated CartReq cartReq) {
        List<CartVO> cartVOList = cartService.update(UserFilter.currentUser.getId(), cartReq.getProductId(), cartReq.getCount());
        return ApiRestResponse.success(cartVOList);

    }

    @ApiOperation("删除购物车")
    @PostMapping("/delete")
    public ApiRestResponse delete(@RequestParam Integer productId) {
        //不能传入userId,cartId,否则可以删除别人的购物车
        List<CartVO> cartVOList = cartService.delete(UserFilter.currentUser.getId(), productId);
        return ApiRestResponse.success(cartVOList);

    }

    @ApiOperation("选择/不选择购物车的某商品")
    @PostMapping("/select")
    public ApiRestResponse select(@RequestBody CartSelectReq cartSelectReq) {//selected选中状态
        //不能传入userId,cartId,否则可以删除别人的购物车
        List<CartVO> cartVOList = cartService.selectOrNot(UserFilter.currentUser.getId(), cartSelectReq.getProductId(), cartSelectReq.getSelected());
        return ApiRestResponse.success(cartVOList);

    }

    @ApiOperation("全选择/全不选择购物车的某商品")
    @PostMapping("/selectAll")
    public ApiRestResponse selectAll(@RequestParam Integer selected) {//selected选中状态
        //不能传入userId,cartId,否则可以删除别人的购物车
        List<CartVO> cartVOList = cartService.selectAllOrNot(UserFilter.currentUser.getId(), selected);
        return ApiRestResponse.success(cartVOList);

    }

}
